package cn.tedu.anjiadb.controller;

import cn.tedu.anjiadb.pojo.dto.UserAddNewDTO;
import cn.tedu.anjiadb.pojo.dto.UserLoginInfoDTO;
import cn.tedu.anjiadb.pojo.dto.UserUpdateDTO;
import cn.tedu.anjiadb.pojo.vo.ProductListItemVO;
import cn.tedu.anjiadb.pojo.vo.UserStandardVO;
import cn.tedu.anjiadb.security.LoginPrincipal;
import cn.tedu.anjiadb.service.IUserService;
import cn.tedu.anjiadb.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/users")
@CrossOrigin
@Api(tags = "01. 用户管理模块")
public class UserController {
    @Autowired
    private IUserService userService;

    @ApiOperation("管理员登录")
    @ApiOperationSupport(order = 10)
    @PostMapping("/login")
    public JsonResult login(UserLoginInfoDTO userLoginInfoDTO) {
        log.debug("开始处理【管理员登录】的请求，参数：{}", userLoginInfoDTO);

        String jwt = userService.login(userLoginInfoDTO);

        return JsonResult.ok(jwt);
    }

    @GetMapping(" ")
    @ApiOperation("根据当前用户id查询用户信息")
    @ApiOperationSupport(order = 20)
    public JsonResult<UserStandardVO> user( @ApiIgnore @AuthenticationPrincipal LoginPrincipal loginPrincipal){
        log.debug("当事人信息：{}", loginPrincipal);
        log.debug("当事人信息中的ID：{}", loginPrincipal.getId());
        log.debug("当事人信息中的用户名：{}", loginPrincipal.getUsername());
        Long id = loginPrincipal.getId();
        UserStandardVO userStandardVO =  userService.getStandardById(id);
        return JsonResult.ok(userStandardVO);
    }

    @PostMapping("/add-new")
    @ApiOperation("用户注册")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(@Valid UserAddNewDTO userAddNewDTO) {
        log.debug("开始处理【用户注册】的请求，参数：{}", userAddNewDTO);
        userService.addNew(userAddNewDTO);
        return JsonResult.ok();
    }

    @PostMapping("/delete")
    @ApiOperation("根据当前用户id注销用户")
    @ApiOperationSupport(order = 200)
    public JsonResult delete(@ApiIgnore @AuthenticationPrincipal LoginPrincipal loginPrincipal){
        log.debug("当事人信息：{}", loginPrincipal);
        log.debug("当事人信息中的ID：{}", loginPrincipal.getId());
        log.debug("当事人信息中的用户名：{}", loginPrincipal.getUsername());
        Long id = loginPrincipal.getId();
        userService.deleteById(id);
        return JsonResult.ok();
    }

    @PostMapping("/update")
    @ApiOperation("根据当前用户id修改用户信息")
    @ApiOperationSupport(order = 300)
    public JsonResult update(@ApiIgnore @AuthenticationPrincipal LoginPrincipal loginPrincipal,UserUpdateDTO userUpdateDTO){
        log.debug("当事人信息：{}", loginPrincipal);
        log.debug("当事人信息中的ID：{}", loginPrincipal.getId());
        log.debug("当事人信息中的用户名：{}", loginPrincipal.getUsername());
        Long id = loginPrincipal.getId();
        userService.updateInfoById(id,userUpdateDTO);
        return JsonResult.ok();
    }

    @GetMapping("/collect")
    @ApiOperation("根据当前用户id查询收藏列表")
    @ApiOperationSupport(order = 500)
    public JsonResult getProductListById(@ApiIgnore @AuthenticationPrincipal LoginPrincipal loginPrincipal){
        log.debug("当事人信息：{}", loginPrincipal);
        log.debug("当事人信息中的ID：{}", loginPrincipal.getId());
        log.debug("当事人信息中的用户名：{}", loginPrincipal.getUsername());
        Long id = loginPrincipal.getId();
        List<ProductListItemVO> list =  userService.getProductListById(id);
        return JsonResult.ok(list);
    }
}
